package yatan.common.proxypool;

import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import yatan.common.pageretriever.PageRetriever;

public class ProxyListRetriever {
    private Map<String, String> numberMap = new HashMap<String, String>();

    public static void main(String[] args) throws Exception {
        new ProxyListRetriever().retrieveProxies();
    }

    public void retrieveProxies() throws Exception {
        List<String> proxies = new ArrayList<String>();

        PageRetriever pageRetriever = new PageRetriever();
        pageRetriever.setUseProxy(false);
        for (int i = 0; i < 10; i++) {
            String pageContent =
                    pageRetriever.getPageContent("http://www.cnproxy.com/proxy" + Integer.toString(i + 1) + ".html");

            Matcher matcher =
                    Pattern.compile(
                            "([a-z]=\"[0-9]\");([a-z]=\"[0-9]\");([a-z]=\"[0-9]\");([a-z]=\"[0-9]\");([a-z]=\"[0-9]\");([a-z]=\"[0-9]\");([a-z]=\"[0-9]\");([a-z]=\"[0-9]\");([a-z]=\"[0-9]\");([a-z]=\"[0-9]\");")
                            .matcher(pageContent);
            Map<String, String> numberMap = new HashMap<String, String>();
            if (!matcher.find()) {
                throw new Exception("No number map line found");
            }
            for (int j = 0; j < 10; j++) {
                String[] entry = matcher.group(j + 1).split("=");
                numberMap.put(entry[0], String.valueOf(entry[1].charAt(1)));
            }

            matcher =
                    Pattern.compile("<tr><td>(.+?)<SCRIPT type=text/javascript>document.write\\(\":\"(.+?)\\)</SCRIPT>")
                            .matcher(pageContent);
            while (matcher.find()) {
                String port = matcher.group(2);
                for (Entry<String, String> entry : numberMap.entrySet()) {
                    port = port.replaceAll("\\+" + entry.getKey(), entry.getValue());
                }
                if (port.length() <= 4) {
                    proxies.add(matcher.group(1) + ":" + port);
                }
            }
        }

        System.out.println(proxies.size());

        FileWriter writer = new FileWriter("proxy/auto-proxy.txt");
        try {
            for (String line : proxies) {
                writer.write(line + "\n");
            }
        } finally {
            writer.close();
        }
    }
}
